﻿var zombies = zombies || {};
var windowURL = window.URL || window.webkitURL;

zombies.EditProfile = function () {
    var self = this;

    self.Loaded = ko.observable(false);

    self.Avatar = ko.observable("../Advertise/DefaultAssets/NoAvatar.gif");
    self.FullName = ko.observable();
    self.FirstName = ko.observable();
    self.LastName = ko.observable();
    self.Gender = ko.observable();
    self.Birthday = ko.observable();
    self.CMND = ko.observable();
    self.Email = ko.observable();
    self.Address = ko.observable();
    self.Phone = ko.observable();

    self.UpdateProfile = function () {
        var dataToEdit = {
            FirstName: self.FirstName,
            LastName: self.LastName,
            Gender: self.Gender,
            Birthday: self.Birthday,
            CMND: self.CMND,
            Email: self.Email,
            Address: self.Address,
            Phone: self.Phone
        };

        $.ajax({
            type: "POST",
            url: "/api/MemberProfile/UpdateProfile",
            data: ko.toJSON(dataToEdit), //Convert the Observable Data into JSON
            contentType: "application/json",
            dataType: 'json'
        }).done(function () {
            toastr.success('Cập Nhật Thông Tin Thành Công!');
        }).fail(function (request) {
            var msg = request.responseText.substring(12);
            msg = msg.substring(0, msg.length - 2);
            toastr.error(msg);
        });
    }
};

var load = function (id) {
    $.ajax({
        type: "GET",
        url: "/api/MemberProfile/GetUserProfile",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (item) {
        if (item.Avatar != null && item.Avatar != '') {
            editProfileViewModel.Avatar(item.Avatar);
        }
        editProfileViewModel.FullName(item.FirstName + ' ' + item.LastName);
        editProfileViewModel.FirstName(item.FirstName);
        editProfileViewModel.LastName(item.LastName);
        editProfileViewModel.Gender(item.Gender);
        editProfileViewModel.Birthday(item.Birthday);
        editProfileViewModel.CMND(item.CMND);
        editProfileViewModel.Email(item.Email);
        editProfileViewModel.Address(item.Address);
        editProfileViewModel.Phone(item.Phone);
        editProfileViewModel.Loaded(true);
    });
};

var editProfileViewModel;
$(document).ready(function () {
    editProfileViewModel = new zombies.EditProfile();
    load();
    ko.applyBindings(editProfileViewModel, document.getElementById("my-container"));
});